package com.comleader.ldmapdownload.controller;

import cn.hutool.json.JSONUtil;
import com.comleader.ldmapdownload.bean.Hopper;
import com.comleader.ldmapdownload.bean.RoutePlanEntity;
import com.comleader.ldmapdownload.util.map.HopperUtils;
import com.comleader.ldmapdownload.util.TimeUtils;
import com.graphhopper.PathWrapper;
import com.graphhopper.util.GPXEntry;
import com.graphhopper.util.shapes.GHPoint;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Hello world!
 * 线路规划的工具类
 */
@CrossOrigin
@RestController
@RequestMapping("/routePlan")
public class RoutePlanController {

    /**
     * @description 线路规划
     * @author zhanghang
     * @date 2020-09-17 15:11:37
	 * @param data : 请求的点，格式[{lat:0000,lon:0000},{lat:0000,lon:0000}]
     * 线路规划的参数:
     * @return
     **/
    @RequestMapping(value = "/plan")
    public Map get(@RequestBody String data){
        RoutePlanEntity routePlanEntity = JSONUtil.toBean(data, RoutePlanEntity.class);
        HashMap<Object, Object> map = new HashMap<>();
        System.out.println("points>>>>"+routePlanEntity.getPoints());
        List<GHPoint> ghPoints = HopperUtils.points(routePlanEntity.getPoints());
        System.out.println("ghPoints>>>>"+ghPoints);
        Hopper hopper = routePlanEntity.getHopper();
        try {
            PathWrapper path = HopperUtils.path(ghPoints, hopper);
            // 或获取json
            // 路线信息
            List<Map<String, Object>> iList = path.getInstructions().createJson();
            // 或以gpx条目的形式获取结果：
            List<GPXEntry> list = path.getInstructions().createGPXList();
            // 路线规划坐标点集合
            List list1 = HopperUtils.gpxList(list);
            map.put("gpx", list1);
            map.put("time", TimeUtils.getGapTime(path.getTime()));
            map.put("path", iList);
        } catch (Exception e) {
            map.put("msg","请到城市或者公路上使用此功能");
            e.printStackTrace();
        }
        return map;
    }
}